﻿using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace CloudStorageLight.Core
{

    public class AttachedSharePointBlobAdapter : SpsBlobAdapter
    {

        public static void SetSpsClientContext(ClientContext context)
        {
            HttpContext.Current.Session["AttachedSharePointBlobAdapter.SpsClientContext"] = context;
        }

        public static ClientContext GetSpsClientContext()
        {
            var val = (ClientContext)HttpContext.Current.Session["AttachedSharePointBlobAdapter.SpsClientContext"];
            return val;
        }

        protected override ClientContext SpsClientContext
        {
            get
            {
                if (_SpsClientContext == null)
                {
                    if (HttpContext.Current == null || HttpContext.Current.Session == null)
                    {
                        _SpsClientContext = base.SpsClientContext;
                    }
                    else
                    {
                        _SpsClientContext = GetSpsClientContext();
                        if (_SpsClientContext == null) _SpsClientContext = base.SpsClientContext;
                    }
                }
                return _SpsClientContext;
	        }
        }
        private ClientContext _SpsClientContext;
        

        public List<List> DocumentLibraryList
        {
            get 
            {
                if (_documentLibraryList == null)
                {
                    var web = SpsClientContext.Web;
                    SpsClientContext.Load(web);
                    SpsClientContext.ExecuteQuery();

                    ListCollection lists = web.Lists;
                    SpsClientContext.Load<ListCollection>(lists);
                    SpsClientContext.ExecuteQuery();

                    _documentLibraryList = lists.Where(x => x.BaseType.ToString() == "DocumentLibrary").ToList();
                }
                return _documentLibraryList; 
            }
        }
        private List<List> _documentLibraryList;

        public AttachedSharePointBlobAdapter(string rootPath, string spSiteUrl, string docLib, string user, string password, string timezone = null)
            : base(rootPath, spSiteUrl, user, password, docLib, timezone)
        {
        }

        public override string GetOriginalUrl(string path)
        {
            return SpSiteUrl.TrimEnd('/') + "/_layouts/15/download.aspx?SourceUrl=" + HttpUtility.UrlEncode(ConvertUrlPathToSharePointPath(path));
        }

        bool disposed = false;
        protected override void Dispose(bool disposing)
        {
            if (disposed) return;
            if (disposing)
            {
                if (_SpsClientContext != null) _SpsClientContext.Dispose();
            }

            disposed = true;
        }

    }

}
